//
//  PortraintTestViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/7.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "PortraintTestViewController.h"
#import "PotraitAnimationController.h"
#import "PortraitPresentationController.h"

@interface PortraintTestViewController ()<UIViewControllerTransitioningDelegate>
@property (strong, nonatomic) UIPercentDrivenInteractiveTransition *interactiveController;
@property (assign, nonatomic) BOOL interactiving;

@end

@implementation PortraintTestViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addGuesture];
}

- (void)addGuesture {
    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:recognizer];
}

- (void)pan:(UIPanGestureRecognizer *)recognizer {
    CGFloat y = [recognizer translationInView:recognizer.view].y;
    CGFloat progress = MAX(0, MIN(1, y / CGRectGetHeight(self.view.frame)));
    
    NSLog(@"%lf, %lf", progress, y);
    
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.interactiving = YES;
            self.interactiveController = [[UIPercentDrivenInteractiveTransition alloc] init];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            [self.interactiveController updateInteractiveTransition:progress];
        }
            break;
        case UIGestureRecognizerStateCancelled:
        {
            self.interactiving = NO;
            [self.interactiveController cancelInteractiveTransition];
            self.interactiveController = nil;
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            self.interactiving = NO;
            
            if (progress > .5f) {
                [self.interactiveController finishInteractiveTransition];
            }
            else {
                [self.interactiveController cancelInteractiveTransition];
            }
            self.interactiveController = nil;
        }
            break;
        default:
            break;
    }
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
    return self.interactiving ? self.interactiveController : nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[PotraitAnimationController alloc] init];
}


- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [[PotraitAnimationController alloc] init];
}


- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
    return [[PortraitPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

@end
